/*   1:    */ package org.jetbrains.jps.model.serialization.library;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.util.JDOMUtil;
import com.intellij.util.containers.MultiMap;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.*;
import org.jetbrains.jps.model.java.JpsJavaLibraryType;
import org.jetbrains.jps.model.library.*;
import org.jetbrains.jps.model.library.JpsLibraryRoot.InclusionOptions;
import org.jetbrains.jps.model.library.sdk.JpsSdkType;
import org.jetbrains.jps.model.module.JpsModuleReference;
import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;

import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */ 
/*  33:    */ public class JpsLibraryTableSerializer
/*  34:    */ {
/*  35: 36 */   private static final JpsLibraryRootTypeSerializer[] a = { new JpsLibraryRootTypeSerializer("CLASSES", JpsOrderRootType.COMPILED, true), new JpsLibraryRootTypeSerializer("SOURCES", JpsOrderRootType.SOURCES, true) };
/*  36:    */   private static final String b = "name";
/*  37:    */   private static final String c = "type";
/*  38:    */   private static final String d = "properties";
/*  39:    */   private static final String e = "jarDirectory";
/*  40:    */   private static final String f = "url";
/*  41:    */   private static final String g = "root";
/*  42:    */   private static final String h = "recursive";
/*  43:    */   private static final String i = "library";
/*  44: 48 */   private static final JpsLibraryPropertiesSerializer<JpsDummyElement> j = new JpsLibraryPropertiesSerializer(JpsJavaLibraryType.INSTANCE, null)
/*  45:    */   {
/*  46:    */     public JpsDummyElement loadProperties(@Nullable Element paramAnonymousElement)
/*  47:    */     {
/*  48: 52 */       return JpsElementFactory.getInstance().createDummyElement();
/*  49:    */     }
/*  50:    */     
/*  51:    */     public void saveProperties(JpsDummyElement paramAnonymousJpsDummyElement, Element paramAnonymousElement) {}
/*  52:    */   };
/*  53:    */   private static final String k = "module";
/*  54:    */   private static final String l = "project";
/*  55:    */   private static final String m = "application";
/*  56:    */   
/*  57:    */   public static void loadLibraries(@Nullable Element paramElement, JpsLibraryCollection paramJpsLibraryCollection)
/*  58:    */   {
/*  59: 64 */     for (Element localElement : JDOMUtil.getChildren(paramElement, "library")) {
/*  60: 65 */       paramJpsLibraryCollection.addLibrary(loadLibrary(localElement));
/*  61:    */     }
/*  62:    */   }
/*  63:    */   
/*  64:    */   public static void saveLibraries(JpsLibraryCollection paramJpsLibraryCollection, Element paramElement)
/*  65:    */   {
/*  66: 70 */     ArrayList localArrayList = new ArrayList();
/*  67: 71 */     for (Iterator localIterator = paramJpsLibraryCollection.getLibraries().iterator(); localIterator.hasNext();)
/*  68:    */     {
/*  69: 71 */       localJpsLibrary = (JpsLibrary)localIterator.next();
/*  70: 72 */       if (!(localJpsLibrary.getType() instanceof JpsSdkType)) {
/*  71: 73 */         localArrayList.add(localJpsLibrary);
/*  72:    */       }
/*  73:    */     }
/*  74:    */     JpsLibrary localJpsLibrary;
/*  75: 77 */     Collections.sort(localArrayList, new Comparator()
/*  76:    */     {
/*  77:    */       public int compare(JpsLibrary paramAnonymousJpsLibrary1, JpsLibrary paramAnonymousJpsLibrary2)
/*  78:    */       {
/*  79: 80 */         return paramAnonymousJpsLibrary1.getName().compareToIgnoreCase(paramAnonymousJpsLibrary2.getName());
/*  80:    */       }
/*  81:    */     });
/*  82: 84 */     for (localIterator = localArrayList.iterator(); localIterator.hasNext();)
/*  83:    */     {
/*  84: 84 */       localJpsLibrary = (JpsLibrary)localIterator.next();
/*  85: 85 */       Element localElement = new Element("library");
/*  86: 86 */       saveLibrary(localJpsLibrary, localElement, localJpsLibrary.getName());
/*  87: 87 */       paramElement.addContent(localElement);
/*  88:    */     }
/*  89:    */   }
/*  90:    */   
/*  91:    */   public static JpsLibrary loadLibrary(Element paramElement)
/*  92:    */   {
/*  93: 92 */     return loadLibrary(paramElement, paramElement.getAttributeValue("name"));
/*  94:    */   }
/*  95:    */   
/*  96:    */   public static JpsLibrary loadLibrary(Element paramElement, String paramString)
/*  97:    */   {
/*  98: 96 */     String str1 = paramElement.getAttributeValue("type");
/*  99: 97 */     JpsLibraryPropertiesSerializer localJpsLibraryPropertiesSerializer = b(str1);
/* 100: 98 */     JpsLibrary localJpsLibrary = a(paramString, localJpsLibraryPropertiesSerializer, paramElement.getChild("properties"));
/* 101:    */     
/* 102:100 */     MultiMap localMultiMap1 = new MultiMap();
/* 103:101 */     MultiMap localMultiMap2 = new MultiMap();
/* 104:102 */     for (Iterator localIterator = JDOMUtil.getChildren(paramElement, "jarDirectory").iterator(); localIterator.hasNext();)
/* 105:    */     {
/* 106:102 */       localElement1 = (Element)localIterator.next();
/* 107:103 */       str2 = localElement1.getAttributeValue("url");
/* 108:104 */       localObject1 = localElement1.getAttributeValue("type");
/* 109:105 */       localObject2 = localObject1 != null ? a((String)localObject1) : JpsOrderRootType.COMPILED;
/* 110:106 */       boolean bool1 = Boolean.parseBoolean(localElement1.getAttributeValue("recursive"));
/* 111:107 */       localMultiMap1.putValue(localObject2, str2);
/* 112:108 */       if (bool1) {
/* 113:109 */         localMultiMap2.putValue(localObject2, str2);
/* 114:    */       }
/* 115:    */     }
/* 116:    */     Element localElement1;
/* 117:    */     String str2;
/* 118:    */     Object localObject1;
/* 119:    */     Object localObject2;
/* 120:112 */     for (localIterator = JDOMUtil.getChildren(paramElement).iterator(); localIterator.hasNext();)
/* 121:    */     {
/* 122:112 */       localElement1 = (Element)localIterator.next();
/* 123:113 */       str2 = localElement1.getName();
/* 124:114 */       if (!str2.equals("jarDirectory"))
/* 125:    */       {
/* 126:115 */         localObject1 = a(str2);
/* 127:116 */         for (localObject2 = JDOMUtil.getChildren(localElement1, "root").iterator(); ((Iterator)localObject2).hasNext();)
/* 128:    */         {
/* 129:116 */           Element localElement2 = (Element)((Iterator)localObject2).next();
/* 130:117 */           String str3 = localElement2.getAttributeValue("url");
/* 131:    */           JpsLibraryRoot.InclusionOptions localInclusionOptions;
/* 132:119 */           if (localMultiMap1.get(localObject1).contains(str3))
/* 133:    */           {
/* 134:120 */             boolean bool2 = localMultiMap2.get(localObject1).contains(str3);
/* 135:121 */             localInclusionOptions = bool2 ? JpsLibraryRoot.InclusionOptions.ARCHIVES_UNDER_ROOT_RECURSIVELY : JpsLibraryRoot.InclusionOptions.ARCHIVES_UNDER_ROOT;
/* 136:    */           }
/* 137:    */           else
/* 138:    */           {
/* 139:124 */             localInclusionOptions = JpsLibraryRoot.InclusionOptions.ROOT_ITSELF;
/* 140:    */           }
/* 141:126 */           localJpsLibrary.addRoot(str3, (JpsOrderRootType)localObject1, localInclusionOptions);
/* 142:    */         }
/* 143:    */       }
/* 144:    */     }
/* 145:130 */     return localJpsLibrary;
/* 146:    */   }
/* 147:    */   
/* 148:    */   public static void saveLibrary(JpsLibrary paramJpsLibrary, Element paramElement, String paramString)
/* 149:    */   {
/* 150:134 */     if (paramString != null) {
/* 151:135 */       paramElement.setAttribute("name", paramString);
/* 152:    */     }
/* 153:137 */     a((JpsTypedLibrary)paramJpsLibrary, paramElement);
/* 154:138 */     ArrayList localArrayList = new ArrayList();
/* 155:139 */     for (JpsLibraryRootTypeSerializer localJpsLibraryRootTypeSerializer : a())
/* 156:    */     {
/* 157:140 */       List localList = paramJpsLibrary.getRoots(localJpsLibraryRootTypeSerializer.getType());
/* 158:141 */       if ((!localList.isEmpty()) || (localJpsLibraryRootTypeSerializer.isWriteIfEmpty()))
/* 159:    */       {
/* 160:143 */         Element localElement1 = new Element(localJpsLibraryRootTypeSerializer.getTypeId());
/* 161:144 */         for (JpsLibraryRoot localJpsLibraryRoot : localList)
/* 162:    */         {
/* 163:145 */           localElement1.addContent(new Element("root").setAttribute("url", localJpsLibraryRoot.getUrl()));
/* 164:146 */           if (localJpsLibraryRoot.getInclusionOptions() != JpsLibraryRoot.InclusionOptions.ROOT_ITSELF)
/* 165:    */           {
/* 166:147 */             Element localElement2 = new Element("jarDirectory").setAttribute("url", localJpsLibraryRoot.getUrl());
/* 167:148 */             boolean bool = localJpsLibraryRoot.getInclusionOptions() == JpsLibraryRoot.InclusionOptions.ARCHIVES_UNDER_ROOT_RECURSIVELY;
/* 168:149 */             localElement2.setAttribute("recursive", Boolean.toString(bool));
/* 169:150 */             if (!localJpsLibraryRootTypeSerializer.getType().equals(JpsOrderRootType.COMPILED)) {
/* 170:151 */               localElement2.setAttribute("type", localJpsLibraryRootTypeSerializer.getTypeId());
/* 171:    */             }
/* 172:153 */             localArrayList.add(localElement2);
/* 173:    */           }
/* 174:    */         }
/* 175:156 */         paramElement.addContent(localElement1);
/* 176:    */       }
/* 177:    */     }
/* 178:158 */     paramElement.addContent(localArrayList);
/* 179:    */   }
/* 180:    */   
/* 181:    */   private static <P extends JpsElement> void a(JpsTypedLibrary<P> paramJpsTypedLibrary, Element paramElement)
/* 182:    */   {
/* 183:162 */     JpsLibraryType localJpsLibraryType = paramJpsTypedLibrary.getType();
/* 184:163 */     if (!localJpsLibraryType.equals(JpsJavaLibraryType.INSTANCE))
/* 185:    */     {
/* 186:164 */       JpsLibraryPropertiesSerializer localJpsLibraryPropertiesSerializer = a(localJpsLibraryType);
/* 187:165 */       paramElement.setAttribute("type", localJpsLibraryPropertiesSerializer.getTypeId());
/* 188:166 */       Element localElement = new Element("properties");
/* 189:167 */       localJpsLibraryPropertiesSerializer.saveProperties(paramJpsTypedLibrary.getProperties(), localElement);
/* 190:168 */       if ((!localElement.getContent().isEmpty()) || (!localElement.getAttributes().isEmpty())) {
/* 191:169 */         paramElement.addContent(localElement);
/* 192:    */       }
/* 193:    */     }
/* 194:    */   }
/* 195:    */   
/* 196:    */   private static <P extends JpsElement> JpsLibrary a(String paramString, JpsLibraryPropertiesSerializer<P> paramJpsLibraryPropertiesSerializer, Element paramElement)
/* 197:    */   {
/* 198:176 */     return JpsElementFactory.getInstance().createLibrary(paramString, (JpsLibraryType)paramJpsLibraryPropertiesSerializer.getType(), paramJpsLibraryPropertiesSerializer.loadProperties(paramElement));
/* 199:    */   }
/* 200:    */   
/* 201:    */   private static JpsOrderRootType a(String paramString)
/* 202:    */   {
/* 203:    */     JpsLibraryRootTypeSerializer localJpsLibraryRootTypeSerializer;
/* 204:180 */     for (localJpsLibraryRootTypeSerializer : a) {
/* 205:181 */       if (localJpsLibraryRootTypeSerializer.getTypeId().equals(paramString)) {
/* 206:182 */         return localJpsLibraryRootTypeSerializer.getType();
/* 207:    */       }
/* 208:    */     }
/* 209:185 */     for (??? = JpsModelSerializerExtension.getExtensions().iterator(); ((Iterator)???).hasNext();)
/* 210:    */     {
/* 211:185 */       JpsModelSerializerExtension localJpsModelSerializerExtension = (JpsModelSerializerExtension)((Iterator)???).next();
/* 212:186 */       for (localIterator = localJpsModelSerializerExtension.getLibraryRootTypeSerializers().iterator(); localIterator.hasNext();)
/* 213:    */       {
/* 214:186 */         localJpsLibraryRootTypeSerializer = (JpsLibraryRootTypeSerializer)localIterator.next();
/* 215:187 */         if (localJpsLibraryRootTypeSerializer.getTypeId().equals(paramString)) {
/* 216:188 */           return localJpsLibraryRootTypeSerializer.getType();
/* 217:    */         }
/* 218:    */       }
/* 219:    */     }
/* 220:    */     Iterator localIterator;
/* 221:192 */     return JpsOrderRootType.COMPILED;
/* 222:    */   }
/* 223:    */   
/* 224:    */   private static Collection<JpsLibraryRootTypeSerializer> a()
/* 225:    */   {
/* 226:196 */     ArrayList localArrayList = new ArrayList();
/* 227:197 */     Collections.addAll(localArrayList, a);
/* 228:198 */     for (JpsModelSerializerExtension localJpsModelSerializerExtension : JpsModelSerializerExtension.getExtensions()) {
/* 229:199 */       localArrayList.addAll(localJpsModelSerializerExtension.getLibraryRootTypeSerializers());
/* 230:    */     }
/* 231:201 */     Collections.sort(localArrayList);
/* 232:202 */     return localArrayList;
/* 233:    */   }
/* 234:    */   
/* 235:    */   private static JpsLibraryPropertiesSerializer<?> b(@Nullable String paramString)
/* 236:    */   {
/* 237:206 */     if (paramString != null) {
/* 238:207 */       for (JpsModelSerializerExtension localJpsModelSerializerExtension : JpsModelSerializerExtension.getExtensions()) {
/* 239:208 */         for (JpsLibraryPropertiesSerializer localJpsLibraryPropertiesSerializer : localJpsModelSerializerExtension.getLibraryPropertiesSerializers()) {
/* 240:209 */           if (localJpsLibraryPropertiesSerializer.getTypeId().equals(paramString)) {
/* 241:210 */             return localJpsLibraryPropertiesSerializer;
/* 242:    */           }
/* 243:    */         }
/* 244:    */       }
/* 245:    */     }
/* 246:215 */     return j;
/* 247:    */   }
/* 248:    */   
/* 249:    */   private static <P extends JpsElement> JpsLibraryPropertiesSerializer<P> a(@NotNull JpsLibraryType<P> paramJpsLibraryType)
/* 250:    */   {
/* 251:219 */     if (paramJpsLibraryType == null) {
/* 252:219 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/jps/model/serialization/library/JpsLibraryTableSerializer", "getLibraryPropertiesSerializer" }));
/* 253:    */     }
/* 254:219 */     for (JpsModelSerializerExtension localJpsModelSerializerExtension : JpsModelSerializerExtension.getExtensions()) {
/* 255:220 */       for (JpsLibraryPropertiesSerializer localJpsLibraryPropertiesSerializer : localJpsModelSerializerExtension.getLibraryPropertiesSerializers()) {
/* 256:221 */         if (((JpsLibraryType)localJpsLibraryPropertiesSerializer.getType()).equals(paramJpsLibraryType)) {
/* 257:223 */           return localJpsLibraryPropertiesSerializer;
/* 258:    */         }
/* 259:    */       }
/* 260:    */     }
/* 261:227 */     throw new IllegalArgumentException("unknown type library:" + paramJpsLibraryType);
/* 262:    */   }
/* 263:    */   
/* 264:    */   public static JpsElementReference<? extends JpsCompositeElement> createLibraryTableReference(String paramString)
/* 265:    */   {
/* 266:231 */     JpsElementFactory localJpsElementFactory = JpsElementFactory.getInstance();
/* 267:232 */     if (paramString.equals("project")) {
/* 268:233 */       return localJpsElementFactory.createProjectReference();
/* 269:    */     }
/* 270:235 */     if (paramString.equals("application")) {
/* 271:236 */       return localJpsElementFactory.createGlobalReference();
/* 272:    */     }
/* 273:238 */     for (JpsModelSerializerExtension localJpsModelSerializerExtension : JpsModelSerializerExtension.getExtensions())
/* 274:    */     {
/* 275:239 */       JpsElementReference localJpsElementReference = localJpsModelSerializerExtension.createLibraryTableReference(paramString);
/* 276:240 */       if (localJpsElementReference != null) {
/* 277:241 */         return localJpsElementReference;
/* 278:    */       }
/* 279:    */     }
/* 280:244 */     throw new UnsupportedOperationException();
/* 281:    */   }
/* 282:    */   
/* 283:    */   public static String getLevelId(JpsElementReference<? extends JpsCompositeElement> paramJpsElementReference)
/* 284:    */   {
/* 285:248 */     if ((paramJpsElementReference instanceof JpsModuleReference)) {
/* 286:249 */       return "module";
/* 287:    */     }
/* 288:251 */     JpsCompositeElement localJpsCompositeElement = (JpsCompositeElement)paramJpsElementReference.resolve();
/* 289:252 */     if ((localJpsCompositeElement instanceof JpsProject)) {
/* 290:253 */       return "project";
/* 291:    */     }
/* 292:255 */     if ((localJpsCompositeElement instanceof JpsGlobal)) {
/* 293:256 */       return "application";
/* 294:    */     }
/* 295:258 */     for (JpsModelSerializerExtension localJpsModelSerializerExtension : JpsModelSerializerExtension.getExtensions())
/* 296:    */     {
/* 297:259 */       String str = localJpsModelSerializerExtension.getLibraryTableLevelId(paramJpsElementReference);
/* 298:260 */       if (str != null) {
/* 299:261 */         return str;
/* 300:    */       }
/* 301:    */     }
/* 302:264 */     return null;
/* 303:    */   }
/* 304:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.jps.model.serialization.library.JpsLibraryTableSerializer

 * JD-Core Version:    0.7.0.1

 */